package si.yun.utils;

import com.baidu.aip.ocr.AipOcr;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;


/**
 * @projectName: ThinkManage
 * @package: si.yun.utils
 * @className: baiduOcr
 * @date: 2021/12/24 9:14:21
 * @author: LBF
 */
public class ApiOrcUtil{

    //设置APPID/AK/SK
    private static String APPID = "28914373";
    private static String APIKEY ="zGCkq1GiiDbNVWrjPqTwGusz";
    private static String SECRETKEY = "tV0O1yOPoTmdP4ErCpjPoHcUfCUs2bhZ";

    public static Map<String,Object> getPictureString(String frontPath,String backPath){
        //将在百度云创建的APPID、APIKEY、SECRETKEY，传入其中
        AipOcr client = new AipOcr(APPID,APIKEY,SECRETKEY);
        // 可选：设置网络连接参数
        client.setConnectionTimeoutInMillis(2000);
        client.setSocketTimeoutInMillis(60000);
        // 传入可选参数调用接口
        HashMap<String, String> options = new HashMap<>();
        // 是否检测朝向
        options.put("detect_direction", "false");
        // 是否检测风险
        options.put("detect_risk", "false");
        //正面，由于frontPath是URL图片需要通过io转换为二维图像数据传入其中
        Map<String,Object> front = getFrontAuth(client,Base64Util.imageUrlToBase64(frontPath),"front",options);
        //反面，由于backPath是URL图片需要通过io转换为二维图像数据传入其中
        Map<String,Object> back = getBackAuth(client,Base64Util.imageUrlToBase64(backPath),"back",options);
        if(front != null && back != null){
            return front;
        }
        return null;
    }

    //身份证正面信息获取
    public static Map<String ,Object> getFrontAuth(AipOcr client, byte[] path, String type, HashMap<String, String> options){
        Map<String ,Object> map = new HashMap<>();
        try {
            //获取通过百度AI文字识别获取的身份证信息
            JSONObject res = client.idcard(path, type, options);

            //若image_status为non_idcard，图片不是身份证
            System.out.println("正面身份证：" + res.toString(2));
            if (!"non_idcard".equals(res.get("image_status"))) {
                JSONObject words_result = res.getJSONObject("words_result");
                map.put("name", words_result.getJSONObject("姓名").get("words"));
                map.put("nation", words_result.getJSONObject("民族").get("words"));
                map.put("address", words_result.getJSONObject("住址").get("words"));
                map.put("idCard", words_result.getJSONObject("公民身份号码").get("words"));
                map.put("birth", words_result.getJSONObject("出生").get("words"));
                map.put("gender", words_result.getJSONObject("性别").get("words"));
                return map;
            } else {
                return null;
            }
        }catch (JSONException e){
            e.printStackTrace();
        }
        return null;
    }
    //身份证反面信息获取
    public static Map<String ,Object> getBackAuth(AipOcr client, byte[] path, String type, HashMap<String, String> options){
        Map<String ,Object> map = new HashMap<>();
        try {
            //获取通过百度AI文字识别获取的身份证信息
            JSONObject res = client.idcard(path, type, options);
            //若image_status为non_idcard，图片不是身份证
            System.out.println("反面身份证：" + res.toString(2));
            if (!"non_idcard".equals(res.get("image_status"))) {
                JSONObject words_result = res.getJSONObject("words_result");
                map.put("expirationDate", words_result.getJSONObject("失效日期").get("words"));
                map.put("issuingOrganization", words_result.getJSONObject("签发机关").get("words"));
                map.put("dateOfIssue", words_result.getJSONObject("签发日期").get("words"));
                return map;
            } else {
                return null;
            }
        }catch (JSONException e){
            e.printStackTrace();
        }
        return null;
    }

}
